#include "Switch.h"
#include "main.h"
#include "malloc.h"
#include "ZY_Button.h"
#include "ws2812.h"
#include "rtthread.h"

#define Door_Time  10000
#define Dog_Time   10000

#define sw1_id 1
#define sw2_id 2
#define sw3_id 3
#define sw4_id 4
#define sw5_id 5
#define sw6_id 6
ZY_BUTTON_t    sw1;
ZY_BUTTON_t    sw2;
ZY_BUTTON_t    sw3;
ZY_BUTTON_t    sw4;
ZY_BUTTON_t    sw5;
ZY_BUTTON_t    sw6;

Door_tcb_t door;

Dog_tcb_t  dog;



//门硬件
void EnableControl_door(uint8_t status)
{
	if(status==Open)
	{
		HAL_GPIO_WritePin( SW_OUT1_GPIO_Port,SW_OUT1_Pin,1);
	}
	else
	{
		HAL_GPIO_WritePin( SW_OUT2_GPIO_Port,SW_OUT2_Pin,1);
	}
}
void DisableControl_door()
{
	HAL_GPIO_WritePin( SW_OUT1_GPIO_Port,SW_OUT1_Pin,0);
	HAL_GPIO_WritePin( SW_OUT2_GPIO_Port,SW_OUT2_Pin,0);
}



//自动开关门
void Auto_OpenDoor(uint8_t status)
{
	door.target_status=status;
}
uint8_t GetDoor_Status()
{
	return  door.current_status;
}
void Change_Door(void)
{
	if(dog.status==Dog_IN)
	{
		return;
	}
	if(door.current_status==Open)
	{
		door.target_status=Close;
	}
	else
	{
		door.target_status=Open;
	}
}
void Door_Process(Door_tcb_t *dr,uint16_t time)
{
	if(dr->current_status!=dr->target_status)
	{
		EnableControl_door(dr->target_status);   //
		if(dr->run==0)
		{
			dr->time=0;
			dr->run=1;
		}
	}
	if(dr->time>=Door_Time)
	{
		DisableControl_door();   //
		dr->time=Door_Time;
		if(dr->run==1)
		{
			dr->run=0;
			dr->current_status=dr->target_status;
		}
	}
	dr->time+=time;
}

void Dog_Recive()
{
	dog.time=0;
}

//狗
void Dog_Process(Dog_tcb_t *dr,uint8_t time)
{
	if(dr->time>=Dog_Time)
	{
		//if(GetDoor_Status()!=Close)
		//{
		if(dr->status!=Dog_OUT)
		{
			dr->status=Dog_OUT;
			Auto_OpenDoor(Close);
		}
		//}
		dr->time=Dog_Time;
	}
	else
	{
		dr->status=Dog_IN;
	//	if(GetDoor_Status()!=Open)
		//{
			Auto_OpenDoor(Open);
		//}
	}
	dr->time+=time;
}



/// @brief 开关输入 硬件
/// @param msg 
/// @param arg_int 
/// @return 
uint8_t Get_SW_Key_Pin(uint8_t msg,uint8_t arg_int)
{
	uint8_t key=0;
	uint8_t status=0;
	switch(arg_int)
	{
		case sw1_id:
			key=HAL_GPIO_ReadPin(SW1_GPIO_Port,SW1_Pin)?Button_down:Button_up;
		  break;
		case sw2_id: 
			key=HAL_GPIO_ReadPin(SW2_GPIO_Port,SW2_Pin)?Button_down:Button_up;
		  break;
		case sw3_id: 
			key=HAL_GPIO_ReadPin(SW3_GPIO_Port,SW3_Pin)?Button_down:Button_up;
		  break;
		case sw4_id: 
			key=HAL_GPIO_ReadPin(SW4_GPIO_Port,SW4_Pin)?Button_down:Button_up;
		  break;
		case sw5_id: 
			key=HAL_GPIO_ReadPin(SW5_GPIO_Port,SW5_Pin)?Button_down:Button_up;
		  break;
		case sw6_id: 
			key=HAL_GPIO_ReadPin(SW6_GPIO_Port,SW6_Pin)?Button_down:Button_up;
		  break;
	}
	return key;
}
/// @brief 开关打开回调
/// @param button_id 按键id
void SW_UP(uint8_t button_id)
{
	//SW_Change_Statue(SW_OFF);
}
/// @brief 开关闭合回调
/// @param button_id 按键id
void SW_Down(uint8_t button_id)
{
	switch(button_id)
	{
		case sw1_id:
			Change_Door();
			break;
		case sw2_id:
			//Change_Door();
			break;
		case sw3_id:
			break;
		case sw4_id:
			break;
		case sw5_id:
			break;
		case sw6_id:
			break;
	}
	//SW_Change_Statue(SW_ON);
}
/// @brief 开关初始化
void Switch_Init()
{
	//Init_Reply_Response(&sw_retry);   //初始化重发管理
	ZY_Button_Register_f(&sw1,Get_SW_Key_Pin,200,sw1_id);
	ZY_ButtonRegister_EventKeyUP(&sw1,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw1,SW_Down);
	
	ZY_Button_Register_f(&sw2,Get_SW_Key_Pin,200,sw2_id);
	ZY_ButtonRegister_EventKeyUP(&sw2,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw2,SW_Down);
	
	ZY_Button_Register_f(&sw3,Get_SW_Key_Pin,200,sw3_id);
	ZY_ButtonRegister_EventKeyUP(&sw3,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw3,SW_Down);
	
	ZY_Button_Register_f(&sw4,Get_SW_Key_Pin,200,sw4_id);
	ZY_ButtonRegister_EventKeyUP(&sw4,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw4,SW_Down);
	
	ZY_Button_Register_f(&sw5,Get_SW_Key_Pin,200,sw5_id);
	ZY_ButtonRegister_EventKeyUP(&sw5,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw5,SW_Down);
	
	ZY_Button_Register_f(&sw6,Get_SW_Key_Pin,200,sw6_id);
	ZY_ButtonRegister_EventKeyUP(&sw6,SW_UP);
	ZY_ButtonRegister_EventKeyDown(&sw6,SW_Down);
	
	
	door.current_status=Open;
	door.target_status=Open;
	door.time=0;
	door.run=0;
	
	dog.time=Dog_Time;
}
/// @brief 开关线程
/// @param paremeter 
void SW_thread_entry(void *paremeter)
{
	Switch_Init();
	rt_thread_mdelay(10000);
	while(1)
	{
		ZY_Button_Process(&sw1,20);
	  ZY_Button_Process(&sw2,20);
	  ZY_Button_Process(&sw3,20);
	  ZY_Button_Process(&sw4,20);
	  ZY_Button_Process(&sw5,20);
	  ZY_Button_Process(&sw6,20);
		Door_Process(&door,20);
		Dog_Process(&dog,20);
		if(door.run==0)
		{
		 if(GetDoor_Status()==Open)
		 {
			 Set_Light(Breathe,G_color);
		 }
		 else
		 {
			  Set_Light(Breathe,B_color);
		 }
	  }
		else
		{
			if(door.target_status==Open)
		  {
			 Set_Light(blink,G_color);
		  }
			else
			{
				Set_Light(blink,B_color);
			}
		}
		rt_thread_mdelay(20);
	}
}
